package com.spica.spicaschool.ui.setting

import android.annotation.SuppressLint
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.app.ActivityCompat
import androidx.fragment.app.viewModels
import androidx.navigation.Navigation
import coil.load
import coil.transform.CircleCropTransformation
import com.kongzue.dialogx.dialogs.BottomMenu
import com.kongzue.dialogx.dialogs.TipDialog
import com.kongzue.dialogx.dialogs.WaitDialog
import com.kongzue.dialogx.interfaces.OnIconChangeCallBack
import com.kongzue.dialogx.interfaces.OnMenuItemClickListener
import com.spica.spicaschool.AppHolder
import com.spica.spicaschool.R
import com.spica.spicaschool.base.BaseFragment
import com.spica.spicaschool.databinding.FragmentSettingBinding
import com.spica.spicaschool.ui.about.AboutPage
import com.spica.spicaschool.utils.SpUtils
import com.spica.spicaschool.utils.TOKEN_SIGN
import com.spica.spicaschool.viewmodel.UserInfoViewModel
import dagger.hilt.android.AndroidEntryPoint


/**
 * 设置
 */
@AndroidEntryPoint
class SettingFragment : BaseFragment<FragmentSettingBinding>(), View.OnClickListener {


    private val viewModel: UserInfoViewModel by viewModels()


    @SuppressLint("SetTextI18n")
    private fun initView() {
        WaitDialog.show("更新用户信息中")
        viewBinding.itemAccount.setOnClickListener(this)
        viewBinding.itemCollect.setOnClickListener(this)
        viewBinding.itemExit.setOnClickListener(this)
        viewBinding.itemFans.setOnClickListener(this)
        viewBinding.itemFollow.setOnClickListener(this)
        viewBinding.itemCollect.setOnClickListener(this)
        viewBinding.itemRelease.setOnClickListener(this)
        viewBinding.itemRecord.setOnClickListener(this)
        viewBinding.btnSend.setOnClickListener(this)
        viewBinding.btnMessages.setOnClickListener(this)
        viewBinding.itemAbout.setOnClickListener(this)
    }



    override fun onClick(view: View) {
        val navController =
            Navigation.findNavController(requireActivity(), R.id.fragment_container_view)
        when (view.id) {
            viewBinding.btnSend.id -> {
                //发表
                showDialog()
            }
            viewBinding.itemRecord.id -> {
                // 我的记录
            }
            viewBinding.itemAccount.id -> {
                // 我的信息
                navController.navigate(SettingFragmentDirections.actionSettingToSettingAccount())
            }
            viewBinding.itemCollect.id -> {
                // 我的收藏
            }
            viewBinding.itemExit.id -> {
                // 登出
                SpUtils.clearSpValue(context = requireActivity())
                ActivityCompat.finishAffinity(requireActivity())
            }
            viewBinding.itemFans.id -> {
                // 我的粉丝
            }
            viewBinding.itemFollow.id -> {
                // 我关注的
            }
            viewBinding.itemRelease.id -> {
                // 我的发布的
                navController.navigate(SettingFragmentDirections.actionSettingToMyDiary())
            }
            viewBinding.itemAbout.id -> {
                //关于此应用
                startActivity(Intent(requireContext(), AboutPage::class.java))
            }
        }
    }

    private fun showDialog() {
        val navController =
            Navigation.findNavController(requireActivity(), R.id.fragment_container_view)
        BottomMenu.show(arrayOf("写日记", "发照片"))
            .setOnIconChangeCallBack(object : OnIconChangeCallBack(true) {
                override fun getIcon(bottomMenu: BottomMenu, index: Int, menuText: String): Int {
                    when (menuText) {
                        "写动态" -> {
                        }
                        "发图文" -> {
                        }
                    }
                    return 0
                }
            }).onMenuItemClickListener =
            OnMenuItemClickListener { _, text, _ ->
                when (text) {
                    "写日记" -> {
                        navController.navigate(SettingFragmentDirections.actionSettingToDiaryWrite())
                    }
                    "发照片" -> {
                        navController.navigate(SettingFragmentDirections.actionSettingToPhotoAdd())
                    }
                }
                false
            }
    }

    override fun setupViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FragmentSettingBinding = FragmentSettingBinding.inflate(inflater, container, false)

    @SuppressLint("SetTextI18n")
    override fun init() {
        initView()
        val token = SpUtils.getSpValue(
            context = requireContext(),
            key = TOKEN_SIGN,
            defaultVal = ""
        )
        viewModel.fetchUserInfo(
            //传入Token
            token = token,
            //请求完成的处理
            onComplete = {
                //关闭加载框
                WaitDialog.dismiss()
            },
            //请求错误的处理
            onError =
            {
                //打印错误信息
                TipDialog.show(it)
            })

        viewModel.userLiveData.observe(this) {
            AppHolder.user = it
            //监听数据源的变化
            viewBinding.root.post {
                viewBinding.tvNickname.text = it?.uNickname
                viewBinding.ivAvatar
                    .load(it?.uAvatar) {
                        error(R.drawable.default_head)
                        placeholder(R.drawable.default_head)
                        transformations(CircleCropTransformation())
                    }
                viewBinding.tvDesc.text = "${it?.uRank}级 萌新"
            }
        }

    }

}
